/*
This file generates all descriptive evidence related to household lending
in the paper, including the appendix. In particular, it generates:

	- Figure 1.b - Nonbank Debt Share over Time
	- Figure E.1.b - Largest industries among nonbank lenders
	- Figure E.3.b - Map of Nonbank Debt Share
	- Table 1.a - Summary statistics table (households)
*/

* Load household lending dataset
use if delinquency != 1 using "$procdata/urtepers_panel_ALL.dta", clear


////////////////////////////////////////////////////////////////////////////////
//// Figure 1.b - Nonbank Debt Share over Time  

preserve 

gcollapse (sum) REST_GAELD_BLB, by(nonbank_lender year)

bysort year: egen agg_debt = total(REST_GAELD_BLB) 
bysort year: egen agg_nonbankdebt = total(REST_GAELD_BLB) ///
	if nonbank_lender == 1
bysort year: gen nonbank_share = agg_nonbankdebt*100/agg_debt

gcollapse (lastnm) nonbank_share, by(year)

qui su year

tw connected nonbank_share year, ///
	lw(thick) lc(black) mcolor(black) xtitle("")  ///
	ytitle("%", orient(horizontal) size(large)) ///
	yscale(range(0 12)) ylabel(0(2)12, labsize(large)) ///
	xlabel(`r(min)'(2)`r(max)', grid angle(0) labsize(large)) ///
	title("")  name(Fig1_b, replace) legend(off) ///

// Export results
graph export "${figures}/URTEPERS_share_nonbankdebt.eps", ///
	replace fontface("Arial") mag(133)

restore 


////////////////////////////////////////
//// Figure E.1.b - Largest industries among nonbank lenders

preserve 
drop if  mi(nonbank_lender)

// Compute total credit by banks+nonbanks 
egen agg_debt = total(REST_GAELD_BLB)

// Keep only nonbanks, then compute the share of total credit given by each nonbank type
keep if nonbank_lender == 1

gcollapse (sum) REST_GAELD_BLB (last) agg_debt, by(JUR_HOVED_BRA_DB07)

gen share_nonbank_debt = REST_GAELD_BLB*100/agg_debt

*plot top 3 nonbank types
gsort -share_nonbank_debt
generate counter=_n

graph hbar share_nonbank_debt if counter <= 3, ///
	over(JUR_HOVED_BRA_DB07, sort(share_nonbank_debt) descending label(labsize(large))) ///
	bar(1, fcolor(gs10) lcolor(black) lwidth(0.3)) yla(, labsize(large)) /// 
	ytitle("") b1title("%", size(large)) title("") subtitle("") legend(off) ///
	scheme(s1mono) ysize(3) xsize(6) name(E1_b, replace)

// Export
graph export "${figures}/PERS_chart_NonbankIndustries.eps", ///
	replace fontface("Arial") mag(133)

restore 


////////////////////////////////////////////////////////////////////////////////
/// Table 1.a - Summary statistics table (households) 

preserve 

keep if inrange(year,2004,2017) // corresponds to time range in our regressions 

**** Generate various variables to be summarize 
* Identify families with more than 50% of nonbank debt
bysort familie_id year: egen tot_debt=total(REST_GAELD_BLB)
by familie_id year: egen nonbank_volume=total(REST_GAELD_BLB) ///
	if nonbank_lender==1
by familie_id year: gen nonbank_share=nonbank_volume/tot_debt
replace nonbank_share = 0 if nonbank_share == .
by familie_id year: gen nonbank_borrower = 1 if nonbank_share>=0.5
replace nonbank_borrower = 0 if nonbank_borrower == .
label def nonbank_borrower 1 "Nonbank Borrower"  0 "Bank Borrower"

* Compute number of distinct lenders per familie-year
gsort familie_id year id_lender
by familie_id year: gen id_lender2 = id_lender[_n-1] if year != 2003
by familie_id year: egen lender_count = total(cond(id_lender == id_lender2,0,1)) 

by familie_id year: gen cvrnr_nonbank = id_lender if nonbank_lender == 1 
by familie_id year: gen cvrnr_nonbank2 = id_lender[_n-1] if nonbank_lender == 1 & year != 2003
by familie_id year: egen nonbanklender_count = total(cond(cvrnr_nonbank == cvrnr_nonbank2,0,1))

drop id_lender2 cvrnr_nonbank2

gen debt_1k = tot_debt / 1000 
gen dispon_13_1k = famdispon_13 / 1000
gen dispindk_1k = famdispindk / 1000

*label variables
label var debt_1k "Unsecured debt (thsd DKK)"
label var lender_count "Number of lenders"
label var intrate "Interest rate"
label var dispon_13_1k "Disposable income (thsd DKK)"
label var famalder "Age of eldest adult (years)"
label var famledig24mnd "Recently unemployed"
label var nonbanklender_count "Number of nonbank lenders"
label var nonbank_share "Nonbank debt share"

// Run baseline regression to identify observations with bank & nonbank lenders (Khwaja-Mian sample)
xtset id_borrowerlender year
local regspec  inter1-inter5, a(id_borrower#year id_lender) noconstant vce(cluster id_borrowerlender)
qui reghdfe intrate `regspec'
qui gen obs_sample = 1 if e(sample)
qui reghdfe lndebt `regspec'
qui gen obs_sample_debt = 1 if e(sample)
gegen in_sample = max(obs_sample obs_sample_debt), by(id_borrower year)


// Variables to be summarized
local borrower_stats debt_1k intrate nonbank_share  lender_count ///
	nonbanklender_count famalder  dispon_13_1k  famledig24mnd   

***** Collapse to the pers-year level first
gcollapse (mean) `borrower_stats' nonbank_borrower in_sample, ///
	by(id_borrower year) labelformat(#sourcelabel#)

eststo clear

eststo full_sample: quietly estpost summarize `borrower_stats', d
eststo full_nonbanks: quietly estpost summarize `borrower_stats' if nonbank_borrower == 1, d
eststo full_banks: quietly estpost summarize `borrower_stats' if nonbank_borrower == 0, d

// Repeat summary stats, but only for those obs in Khwaja-Mian sample
eststo insample: quietly estpost summarize `borrower_stats' if in_sample == 1, d	
eststo sample_nonbanks: quietly estpost summarize `borrower_stats' if (nonbank_borrower == 1) & (in_sample == 1), d
eststo sample_banks: quietly estpost summarize `borrower_stats' if (nonbank_borrower == 0) & (in_sample == 1), d


local sharedopts cells("count(fmt(%12.0gc)) mean(fmt(%6.2f)) sd(fmt(%8.2fc)) p50(fmt(%6.2f))") nonumbers  noobs nomtitles

esttab full_sample insample, `sharedopts' mgroups("Full dataset" "Borrowers with bank and nonbank lenders", pattern(1 1 ) span) 

esttab full_sample insample using "$tables/Consolidated_DescStat_Borrowers.tex", `sharedopts' ///
		prehead("\begin{tabular}{l*{3}{cccc}} \toprule") ///
		posthead("\hline \\ \multicolumn{4}{l}{\textbf{Panel A. Households}} \\\\[-1ex]") ///
		fragment collabels("Count" "Mean" "Std. Dev" "Median") ///
		mgroups("Full dataset" "Borrowers with bank and nonbank lenders", ///	
		pattern(1 1 ) prefix(\multicolumn{@span}{c}{) ///
		suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		replace label
		
restore 		


////////////////////////////////////////////////////////////////////////////////
// Figure E.3.a - Map of Nonbank Debt Share (Region) 

preserve

* Sum credit by lender type in each municipality 
* Municipality is based on where the borrower lives

gcollapse (sum) REST_GAELD_BLB, by(municipality nonbank_lender)


bysort municipality: egen agg_debt = total(REST_GAELD_BLB) 
gen share_nonbank_debt = REST_GAELD_BLB / agg_debt if nonbank_lender == 1
keep if nonbank_lender == 1
drop REST_GAELD_BLB nonbank_lender agg_debt

* Cannot create map on remote DST server, export data underlying the map instead
export excel using "$tables/PERS_chart_MuniNonbankShare", firstrow(varlabels) replace
restore 



////////////////////////////////////////////////////////////////////////////////
/// Figure E4.b Heterogeneity in borrowing patterns
	
preserve 
	
rename cvrnr cvrnr_lender
drop if mi(nonbank_lender)
merge m:1 cvrnr_lender year using "$procdata/final_Experian.dta", nogen keep(match master) 

	 
// Identify individuals with bank and nonbank debt 
gegen aux_bank_credit = total(REST_GAELD_BLB) if nonbank_lender == 0, by(id_borrower year)
gegen aux_nonbank_credit = total(REST_GAELD_BLB) if nonbank_lender == 1, by(id_borrower year)
gegen tot_bank_credit = max(aux_bank_credit) , by(id_borrower year)
replace tot_bank_credit = 0 if mi(tot_bank_credit)
gegen tot_nonbank_credit = max(aux_nonbank_credit), by(id_borrower year)
replace tot_nonbank_credit = 0 if mi(tot_nonbank_credit)
gegen tot_pers_credit = total(REST_GAELD_BLB), by(id_borrower year)
gen nonbank_share = tot_nonbank_credit / tot_pers_credit
replace  nonbank_share = 0 if mi(nonbank_share)

gen co_borrower = 0 if nonbank_share == 1 | nonbank_share == 0
replace co_borrower = 1 if tot_bank_credit > 0 & tot_nonbank_credit > 0 & !mi(tot_bank_credit) & !mi(tot_nonbank_credit)

gen only_nonbank = (nonbank_share == 1) 

** Collapse to individual-year level 
gcollapse (max) co_borrower nonbank_share only_nonbank    ///
	(lastnm)  tot_pers_credit famdispon_13 , by(id_borrower year)

* Generate deciles of disposable income distribution 
gquantiles famdispon_deciles = famdispon_13, xtile n(10) by(year)

** Collapse to income decile-level  
drop if tot_pers_credit == 0
replace co_borrower = 0 if mi(co_borrower)
gcollapse (mean) nonbank_share co_borrower only_nonbank , by(famdispon_deciles)

replace nonbank_share = nonbank_share * 100
replace co_borrower = co_borrower * 100
replace only_nonbank = only_nonbank * 100

label define dispinc_lab 1 "P0-P10" 2 "P10-P20" 3 "P20-P30" 4 "P30-P40" 5 "P40-P50" 6 "P50-P60"  7 "P60-P70" 8 "P70-P80" 9 "P80-P90" 10 "P90-P100" , replace 
label values famdispon_deciles dispinc_lab

** Plot results 
graph bar co_borrower only_nonbank nonbank_share, ///
	over(famdispon_deciles, lab(angle(45) labsize(medlarge))) /// 
	title("") ysize(4) xsize(8) name(E4, replace) ///
	ytitle("%", height(4) size(large) orient(horizontal)) /// 
	b1title("Sales percentiles", height(4) size(large)) /// 
	ylabel(0(5)30, labsize(large) format(%9.0f) grid) /// 
	legend(order(1 "Share of HHs w. bank & nonbank credit" ///
		2 "Share of HHs w. only nonbank credit" ///
		3 "Nonbank credit share") ///
		nobox position(11) ring(0) cols(1) size(3) ///
		region(lstyle(none) style(outline))) ///
	bar(1, fcolor(gs13) lcolor(black) lwidth(0.3)) ///
	bar(2, fcolor(gs8) lcolor(black) lwidth(0.3)) ///
	bar(3, fcolor(none) lcolor(black) lwidth(0.6)) 
	
graph export "${figures}/pers_coborrowing_famdispon.eps", ///
	replace fontface("Arial") mag(133)
	 
restore	
		



